<?php
namespace Libs\Common;
/*
 * Class Billing iOS
 * @author ThamLT
 */
class BillingiOSCommon
{
    /**
     * Verify receipt on apple store
     * @param receipt code, issandbox
     * @return: false if fail to connect or invalid receive
     *          array: quantity, productId, transactionId, purchaseDate, appItemId
     *          //if buy point withou authen by password
     * @author ThamLT
     * */
    public static function runReceiptData($receipt, $isPoint = false, $isSandbox = false)
    {
        $sCheckingUrl = 'https://buy.itunes.apple.com/verifyReceipt';
        /*if ($isSandbox) {
            $sCheckingUrl = 'https://sandbox.itunes.apple.com/verifyReceipt';
        }*/
        $urlSandbox = 'https://sandbox.itunes.apple.com/verifyReceipt';

        $postData = array(
                'receipt-data'  => ($receipt),//receive had base64_encode
                //'password'      => $this->_passwordConfirm
        );
        if (!$isPoint) {
            $postData['password'] = '918fe50872c54a5fb0d0d04bcdf27360';
        }
        $postData = json_encode($postData);

        $ch = curl_init($sCheckingUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

        $response = curl_exec($ch);
        $errno    = curl_errno($ch);
        $errmsg   = curl_error($ch);
        curl_close($ch);
        unset($ch);

        if ($errno != 0) {
            return false;
        }
        $data = json_decode($response);
        //status of response is not exist or != 0
        if (!is_object($data) || !isset($data->status) || $data->status != 0) {

            //check status if on test environment
            if ((int)$data->status == 21007) {
                $ch = curl_init($urlSandbox);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

                $response = curl_exec($ch);
                $errno    = curl_errno($ch);
                $errmsg   = curl_error($ch);
                curl_close($ch);

                unset($data);
                $data = json_decode($response);
                if (!is_object($data) || !isset($data->status) || $data->status != 0) {
                    return false;
                }
            } else {
                return false;
            }
        }

        $data = array(
            'receipt'      =>  base64_decode($receipt),
            'quantity'      =>  $data->receipt->quantity,
            'productId'     =>  $data->receipt->product_id,
            'transactionId' =>  $data->receipt->transaction_id,
            'purchaseDate'  =>  $data->receipt->purchase_date,
            //'appItemId'     =>  $data->receipt->app_item_id,
            //'expiresDate'     =>  $data->receipt->expires_date//rely expire date to check expire time of receipt
        );
        if (!empty($data->receipt->expires_date)) {
            //check expire datetime with timezone is tokyo
            $data['expiresDate'] = $data->receipt->expires_date;
            $expireTime = strtotime($data['expiresDate']);
            date_default_timezone_set('Asia/Tokyo');
            $expireTime = strtotime(date('Y-m-d H:i:s', $expireTime));//get expire time with zone is tokyo
            $curTime = time();
            if ($curTime < $expireTime) {
                return false;
            }
        }
        return $data;
    }
}